// pages/components/date-picker.js

import {
  dateTool
} from '../../../tools/date'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    placeholder: {
      type: String,
      value: '选择日期',
    },
    plain: {
      type: Boolean,
      value: false
    },
    theme: {
      type: String,
      value: ''
    },
    "customStyle": String,
    timestamp: {
      type: Number,
      value: 0,
      observer: function (timestamp) {
        this.initTimestamp(timestamp)
      }
    },
    txtDate: {
      type: String,
      value: '',
      observer: function (txt) {
        this.initPicker()
      }
    }
  },
  data: {
    pickerIndexList: [0, 0, 0], //日期选择器下标
    isShowDateLayer: false, //是否显示日期弹层
    txtDate: '选择日期', //选中日期
    isSeltDate: false, //是否选择日期
    yearList: [],
    monthList: [],
    dayList: [],
    globalData: {},
  },

  lifetimes: {
    ready() {
      this.initPicker()
    }
  },

  methods: {
    onPickTime(e) {
      this.showDatePicker()
    },
    initPicker() {
      this.getCurrentDate()
      this.setData({
          pickerIndexList: this.data.pickerIndexList
        })
        // 初始值
        !this.initTimestamp(this.properties.timestamp) &&
        this.initTxt(this.properties.txtDate)
    },
    initTimestamp(timestamp) {
      timestamp = Number(timestamp)
      if (!isNaN(timestamp) && timestamp > 0) {
        const txtDate = dateTool.format(timestamp, 'yyyy-MM-dd')
        this.setData({
          txtDate
        })
        return true
      }
      return false
    },
    initTxt(txt) {
      txt &&
        this.setData({
          txtDate: txt
        })
    },
    //选择日期弹层确定按钮
    bindConfirmDate: function (e) {
      var that = this;
      const {
        yearList,
        monthList,
        dayList
      } = that.data
      var pickerIndexList = that.data.pickerIndexList;
      var txtDate = yearList && monthList && dayList ? yearList[pickerIndexList[0]] + '-' + monthList[pickerIndexList[1]] + '-' + dayList[pickerIndexList[2]] : '';
      that.setData({
        isShowDateLayer: false,
        pickerIndexList,
        txtDate,
        isSeltDate: true
      })
      const param = {
        txt: txtDate,
        timestamp: dateTool.parse(txtDate)
      }
      this.triggerEvent('confirm', param)
      this.triggerEvent('datechange', param)
      this.triggerEvent('visibleChange', {
        visible: false
      })
    },
    showDatePicker() {
      const that = this;
      const pickerIndexList = that.data.pickerIndexList;
      const visible = !that.data.isShowDateLayer
      that.setData({
        isShowDateLayer: visible,
        dayList: that.getDayList(pickerIndexList[0], pickerIndexList[1]),
      })
      this.triggerEvent('visibleChange', {
        visible
      })
    },
    //获取当前日期
    getCurrentDate: function (e) {
      const that = this;
      var yearList = [],
        monthList = [],
        dayList = [];
      const curDate = new Date()
      for (var i = curDate.getFullYear() - 50; i <= curDate.getFullYear() + 50; i++) { //年份
        yearList.push(i);
      }
      for (var i = 1; i <= 12; i++) { //月份
        const m = (i < 10 ? '0' + i : i)
        monthList.push(m);
      }
      var myDate = new Date();
      var currentYearIndex = yearList.findIndex(o => o == myDate.getFullYear());
      var currentMonthIndex = monthList.findIndex(o => o == myDate.getMonth() + 1);
      var dayList = that.getDayList(currentYearIndex, currentMonthIndex); //天
      var currentDayIndex = dayList.findIndex(o => o == myDate.getDate());
      var pickerIndexList = that.data.pickerIndexList;
      pickerIndexList[0] = currentYearIndex;
      pickerIndexList[1] = currentMonthIndex;
      pickerIndexList[2] = currentDayIndex;
      this.data.globalData.dateIndexList = pickerIndexList;
      that.setData({
        yearList,
        monthList,
        dayList,
      })
    },
    //日期选择改变事件
    bindChangeDate: function (e) {
      var that = this;
      var pickerColumnList = e.detail.value;
      that.setData({
        dayList: that.getDayList(pickerColumnList[0], pickerColumnList[1]),
        pickerIndexList: pickerColumnList,
      })
    },
    //获取天数列表
    getDayList: function (year, month) {
      var that = this;
      var dayList;
      switch (month + 1) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
          dayList = that.getDayNum(31);
          break;
        case 4:
        case 6:
        case 9:
        case 11:
          dayList = that.getDayNum(30);
          break;
        case 2:
          dayList = that.getDayNum((that.data.yearList[year] % 4 == 0 && that.data.yearList[year] % 100 != 0 || that.data.yearList[year] % 400 == 0) ? 29 : 28);
          break;
      }
      return dayList;
    },
    //获取天数
    getDayNum: function (num) {
      const dayList = [];
      for (var i = 1; i <= num; i++) {
        const day = (i < 10 ? '0' + i : i)
        dayList.push(day);
      }
      return dayList;
    },
    //选择日期弹层取消按钮
    bindCancelDate: function (e) {
      var that = this;
      var pickerIndexList = that.data.pickerIndexList;
      that.setData({
        isShowDateLayer: !that.data.isShowDateLayer,
      })
      var yearList = that.data.yearList;
      var monthList = that.data.monthList;
      var txtDate = that.data.txtDate || "";
      var yearIndex = yearList.findIndex(o => o == parseInt(txtDate.slice(0, txtDate.indexOf('-')))); //年份下标
      var monthIndex = monthList.findIndex(o => o == parseInt(txtDate.slice(txtDate.indexOf('-') + 1, txtDate.lastIndexOf('-')))); //月份下标
      var dayList = that.getDayList(yearIndex, monthIndex); //天数
      if (yearList && monthList && dayList && that.data.isSeltDate) { //选择过日期
        if (txtDate == (yearList[pickerIndexList[0]] + '-' + monthList[pickerIndexList[1]] + '-' + dayList[pickerIndexList[2]]))
          that.setData({
            pickerIndexList
          })
        else
          that.setData({
            pickerIndexList: [yearIndex, monthIndex, dayList.findIndex(o => o == parseInt(txtDate.slice(txtDate.lastIndexOf('-') + 1, txtDate.length)))]
          })
      } else { //未选择过日期
        that.setData({
          pickerIndexList: this.data.globalData.dateIndexList,
        })
      }

      this.triggerEvent('visibleChange', {
        visible: false
      })
      this.triggerEvent('cancel', {})
    },
  }
})